﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WeifenLuo.WinFormsUI.Docking;
using CustomUIControls;


namespace SalesVMT
{
    public partial class frmCustomerList : DockContent
    {
        int pageSize = UIParamater.PageSize;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        int customer_ID = 0;
        int customertype_ID = 0;

        public int Customer_ID
        {
            get { return customer_ID; }
            set { customer_ID = value; }
        }
        private string customer_Name="";

        public string Customer_Name
        {
            get { return customer_Name; }
            set { customer_Name = value; }
        }

        private bool selected;

        public frmCustomerList(bool _selected)
        {
            InitializeComponent();
            btnDel.Enabled = btnEdit.Enabled = Program.isAdmin;
            DisplayFrame();
            UISetting.LoadCustomerType(ref cboCustomerType);
            LoadData();
            selected = _selected;
        }

        private void DisplayFrame()
        {
            UISetting.AddColumn(ref dgvList1, "Customer_ID", "Khóa", "", 1, false, true);
            UISetting.AddColumn(ref dgvList1, "RowNumber", "STT", "C", ColumnWidth.RowNumber, true, true);
            UISetting.AddColumn(ref dgvList1, "Customer_Code", "Mã số KH", "", ColumnWidth.Code2, true, true);
            UISetting.AddColumn(ref dgvList1, "Customer_Name", "Tên KH", "", ColumnWidth.CustomerName, true, true);
            UISetting.AddColumn(ref dgvList1, "Customer_Address", "Địa chỉ", "", ColumnWidth.Address, true, true);
            UISetting.AddColumn(ref dgvList1, "Customer_Phone", "Điện thoại", "",ColumnWidth.Phone, true, true);
            UISetting.AddColumn(ref dgvList1, "Customer_Fax", "Fax", "", ColumnWidth.Fax, false, true);
            UISetting.AddColumn(ref dgvList1, "Customer_TaxNo", "MST", "", ColumnWidth.TaxCode, false, true);
            UISetting.AddColumn(ref dgvList1, "Customer_Contact", "Liên hệ", "", ColumnWidth.Contact, true, true);

            
            dgvList1.Columns["Customer_Code"].Frozen = true;
            //dgvList1.Columns["Customer_Code"].DefaultCellStyle.BackColor = Color.DarkCyan;
        }
        private void LoadData()
        {
            customertype_ID = Convert.ToInt32(cboCustomerType.ComboBox.SelectedValue);

            DataTable dt = CustomerDB.GetList(out itemCount, customertype_ID, UISetting.GetKeyWord(txtKeyWord.Text), currPage, pageSize).Tables[0];
            PageIndex();
            dgvList1.Rows.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dgvList1.Rows.Insert(i, 1);
                dgvList1.Rows[i].Cells["Customer_ID"].Value = dt.Rows[i]["Customer_ID"].ToString();
                dgvList1.Rows[i].Cells["Customer_Code"].Value = dt.Rows[i]["Customer_Code"].ToString();
                dgvList1.Rows[i].Cells["Customer_Name"].Value = dt.Rows[i]["Customer_Name"].ToString();
                dgvList1.Rows[i].Cells["Customer_Address"].Value = dt.Rows[i]["Customer_Address"].ToString();
                dgvList1.Rows[i].Cells["Customer_Phone"].Value = dt.Rows[i]["Customer_Phone"].ToString();
                dgvList1.Rows[i].Cells["Customer_Fax"].Value = dt.Rows[i]["Customer_Fax"].ToString();
                dgvList1.Rows[i].Cells["Customer_TaxNo"].Value = dt.Rows[i]["Customer_TaxNo"].ToString();
                dgvList1.Rows[i].Cells["Customer_Contact"].Value = dt.Rows[i]["Customer_Contact"].ToString();
                dgvList1.Rows[i].Cells["RowNumber"].Value = dt.Rows[i]["RowNumber"].ToString();

            }
        }
        private void PageIndex()
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage == 0)
                lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            else
                lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();
        }
        private void btnFirst_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currPage != 0)
                currPage--;
            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (totalPage > 0 && currPage != totalPage - 1)
                currPage++;
            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (totalPage != 0)
                currPage = totalPage - 1;
            LoadData();
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void txtKeyWord_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnFind_Click(null, null);
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgvList1.SelectedCells.Count > 0)
            {
                customer_ID = Convert.ToInt32(dgvList1["Customer_ID", dgvList1.SelectedCells[0].RowIndex].Value);
                if (CustomerDB.Using(customer_ID) > 0)
                {
                    MessageBox.Show("KH này đang sử dụng! Không xóa được.", Message.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (MessageBox.Show(Message.Delete_Question, Message.Info, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (CustomerDB.Delete(customer_ID))
                    {
                        Program.taskbarNotifier.Show(Message.Info, Message.Delete_Complete, UIParamater.TimeToShow, UIParamater.TimeToStay, UIParamater.TimeToHide);
                        LoadData();
                    }
                    else
                    {
                        MessageBox.Show(Message.Delete_Error, Message.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvList1.SelectedCells.Count > 0)
            {
                customer_ID = Convert.ToInt32(dgvList1["Customer_ID", dgvList1.SelectedCells[0].RowIndex].Value);
                Form f = new frmCustomer(customer_ID);
                if (f.ShowDialog(this) == DialogResult.OK)
                {
                    LoadData();
                }
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            Form f = new frmCustomer(0);
            if (f.ShowDialog(this) == DialogResult.OK)
            {
                currPage = 0;
                cboCustomerType.SelectedIndex = 0;
                LoadData();
            }
        }

        private void dgvList1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if (selected)
                {
                    customer_ID = Convert.ToInt32(dgvList1["Customer_ID", dgvList1.SelectedCells[0].RowIndex].Value);
                    customer_Name = dgvList1["Customer_Name", dgvList1.SelectedCells[0].RowIndex].Value.ToString();
                    this.DialogResult = DialogResult.OK;
                }
                else
                    if (Program.isAdmin)
                    {
                        btnEdit_Click(null, null);
                    }
            }
        }

        private void cboCustomerType_SelectedIndexChanged(object sender, EventArgs e)
        {

                currPage = 0;
                LoadData();
            
        }
    }
}